Uec3.AddAndEditWindow=Ext.extend(Ext.UecWindow,{
	constructor:function(config){
		this.params=config.params||{};
		var ds_code_select = new Ext.data.JsonStore({
	    	 	url:Uec3.App.Constants.homeUrl+"/part/queryPart!getPartByCodeImplicitExt.do",
	     		root:'root',
	     		totalProperty:'totalProperty',
        		fields:[{name:'code',type:'string'},{name:'name',type:'string'}]
	 	});
		var codeCombo=new Ext.form.ComboBox({				    				
		  name : 'code',					
		  fieldLabel:'零件编码',
          valueField:'code',
		  displayField : 'code',
		  mode : 'remote',
		  queryParam :'queryParam',
		  queryDelay:500,
		  minChars :2,
		  store : ds_code_select,  	  								 
		  pageSize:10,
		  selectOnFocus : true,
		  triggerAction : 'all',
		  loadingText : '加载中...',	   				
          listWidth:270
		});
		var publicanLevelCombo=Uec3.App.dictionary.getCombo("partproperty_publicanLevel",{hiddenName : 'publicanLevel',fieldLabel:'公开级别'});
		//publicanLevelCombo.getStore().load();
		var sourceCombo=Uec3.App.dictionary.getCombo("part_source",{hiddenName : 'source',fieldLabel:'来源'});
		//sourceCombo.getStore().load();
		var nameField=new Ext.form.TextField({allowBlank : true,name : 'name',fieldLabel:'零件名称',readOnly:true,style:{background:'#dddddd'}});
	  	var measurementUnitField=new Ext.form.TextField({name : 'measurementUnit',fieldLabel:'单位',readOnly:true,style:{background:'#dddddd'}});
	  	var brandField=new Ext.form.TextField({xtype:'textfield',name : 'brand',fieldLabel:'品牌'});
       	var carModelField=new Ext.form.TextField({xtype:'textfield',name:'carModel',fieldLabel:'车型',allowBlank:false});
       	var originField=new Ext.form.TextField({name:'origin',allowBlank : true,fieldLabel:'产地'});
       	var swapCodeField=new Ext.form.TextField({name:'swapCode',fieldLabel:'互换码',allowBlank: true});
       	var minOnHandField=new Ext.form.NumberField({name:'minOnHand',allowNegative: false});
        var maxOnHandField=new Ext.form.NumberField({name:'maxOnHand',allowNegative: false,fieldLabel:'上限库存数'});
        var onHandField=new Ext.form.TextField({name:'onHand',readOnly:true,allowBlank: true,style:{background:'#dddddd'}});
        var onWayField=new Ext.form.NumberField({	name:'onWay',allowBlank: true,readOnly:true,style:{background:'#dddddd'}});
		var costField=new Ext.form.NumberField({name:'cost',readOnly:true,style:{background:'#dddddd'},decimalPrecision:2});
       	var retailPriceField=new Ext.form.NumberField({name : 'retailPrice',decimalPrecision:2,allowBlank: false,allowNegative: false});
		var insurancePriceField=new Ext.form.NumberField({name : 'insurancePrice',allowBlank: true,decimalPrecision:2,allowNegative: false});
		var minPriceField=new Ext.form.NumberField({name : 'minPrice',fieldLabel:'最低价',allowBlank: false,decimalPrecision:2,allowNegative: false});
		var quotationQtyField=new Ext.form.NumberField({name:'quotationQty',maxValue:20,allowBlank: true});
		this.partPropertyForm=new Ext.FormPanel({
			frame:true,
    		border:false,
    	    items:[
    	     	{
       			  xtype:'fieldset',
       			  title:'零件信息',
       			  height:110,
       			  layout:'uec3grid',
   			  	  layoutConfig:{
       			      hGrid:[10,23,10,23,10,23],
       			        vGrid:[33,33,33],
       			      usedGrid:[]
   			      },
   			      defaults:{enableKeyEvents : true},
       			  items:[
       			  		{xtype:'label',text:'零件编码:'},codeCombo,
       			  		{xtype:'label',text:'来源:'},sourceCombo,
       			  		{xtype:'label',text:'零件名称:'},nameField,
       			  		{xtype:'label',text:'单位:'},measurementUnitField,
       			  		{xtype:'label',text:'品牌:'},brandField,
       			  		{xtype:'label',text:'车型:'},carModelField,
       			  		{xtype:'label',text:'产地:'},originField,
       			  		{xtype:'label',text:'互换码:'},swapCodeField
       			  ]
    		
    	     	},{
       			  xtype:'fieldset',
       			  title:'库存信息',
       			  height:90,
       			  layout:'uec3grid',
       			  defaults:{enableKeyEvents : true},
   			  	  layoutConfig:{
   			          hGrid:[10,23,10,23,10,23],
   			          vGrid:[50,50],
   			          usedGrid:[]
   			      },
       			  items:[
       			  {xtype:'label',text:'下限库存数:'},minOnHandField,
       			  {xtype:'label',text:'上限库存数:'},maxOnHandField,
       			  {xtype:'label',text:'库存数:'},onHandField,
       			  {xtype:'label',text:'在途数:'},onWayField,
       			  {xtype:'label',text:'成本价:'},costField
    	     	]},{
       			  xtype:'fieldset',
       			  title:'报价信息',
       			  defaults:{enableKeyEvents : true},
       			  height:90,
       			  layout:'uec3grid',
   			  	  layoutConfig:{
   			          hGrid:[10,23,10,23,10,23],
   			          vGrid:[50,50],
   			          usedGrid:[]
   			      },
       			  items:[
       			  {xtype:'label',text:'媒体价:'},retailPriceField,
       			  {xtype:'label',text:'保险价:'},insurancePriceField,
       			  {xtype:'label',text:'最低价:'},minPriceField,
       			  {xtype:'label',text:'上架数:'},quotationQtyField,
       			  {xtype:'label',text:'公开级别:'},publicanLevelCombo
    	     	]},{xtype:'hidden',name:'id'},
    	     	   {xtype:"hidden",name:'partId'}
       			  ]
		});
		config=Ext.apply(config||{},{
			items:[this.partPropertyForm],
			buttonAlign:'center',
    		buttons:[{
    			text:'保存',
    			iconCls:'icon-save',
    			handler:this.save.createDelegate(this)
    		},{
    			text:'关闭',
    			iconCls:'icon-close',
    			handler:this.funClose.createDelegate(this)
    		}]});
		Uec3.AddAndEditWindow.superclass.constructor.call(this,config);
	},
	save:function(){
		var ajax=Uec3.App.Uec3Ajax;
		var _this=this;
		var form=this.partPropertyForm.getForm();
		if(!form.isValid()){
			return;
		}
		form.findField("code").enable();
		form.findField("source").enable();
		var partProperty=form.getValues();
		if(_this.params.type=="edit"){
			form.findField("code").disable();
			form.findField("source").disable();
		}
		var q = Ext.encode(encodeFilte(partProperty)); 
			ajax.request({
				url : Uec3.App.Constants.homeUrl+ '/part/savePartProperty.do',
				timeout:1800000,
				params : {json : q},
				scope:this,
				success : function(res) {
					var result = Ext.decode(res.responseText);
					if (result.success == "OK") {
						if(!_this.params.isNotAlert){
							Ext.Msg.alert('提示', "保存成功");
							Ext.Msg.hide.defer(600,Ext.MessageBox);
						}
						if(Ext.isFunction(_this.params.fn)){
							var retPP=result.partProperty;
							_this.params.fn(retPP);
						}
						_this.close();
					} else {
						Ext.Msg.alert("提示", result.success);
					}
				}
				
		    });
	},
	initComponent:function(){
		Uec3.AddAndEditWindow.superclass.initComponent.call(this);
	},
	initEvents:function(){
		var _this = this;
		Uec3.AddAndEditWindow.superclass.initEvents.call(this);
		this.on('show',this.funAfterShow,this);
		this.partPropertyForm.getForm().findField("code").on("select",_this.funCodeSelectEvent,_this)
		this.partPropertyForm.getForm().findField("source").on("select",_this.funSourceSelectEvent,_this);
		this.partPropertyForm.getForm().findField("retailPrice").on("blur",_this.funSetCompanyConfigValues,_this);
	},
	funSetCompanyConfigValues:function(){
		Ext.MessageBox.confirm("提示","是否填充默认的保险价和最低价?",function(btn){
			if(btn=="yes"){
				Uec3.App.companyConfig.setValuesWithPartProperty(this.partPropertyForm.getForm());
			}
			this.partPropertyForm.getForm().findField("insurancePrice").focus(true,300);
		},this);
	},
	funCodeSelectEvent:function(){
		var form=this.partPropertyForm.getForm();
		form.findField("source").clearValue();
	},
	funSourceSelectEvent:function(){
		var form=this.partPropertyForm.getForm();
		var source = form.findField("source").getValue();
		var code = form.findField("code").getValue();
		if(!code)return;
	 	Ext.Ajax.request({
		    url :Uec3.App.Constants.homeUrl+"/part/findPartByCodeAndSource.do",
		    params :{json:Ext.encode({code:code,source:source})},
		    method :"post",
		    callback :function(options,success,response){
		         
		    },
		    success:function(response,options){	                	 
		    	  var result = Ext.decode(response.responseText);
		    	  var part = result.part;
		    	  if(part){
		    	  	 form.findField("name").setValue(part.name);
		    	  	 form.findField("measurementUnit").setValue(part.measurementUnit);
		    	  	 form.findField("brand").setValue(part.brand);
		    	  	 form.findField("carModel").setValue(part.carModel);
		    	  	 form.findField("origin").setValue(part.origin);
		    	  }else{
		    	     form.reset();
		    	     form.findField('source').clearValue();
		    	     form.findField('code').clearValue();
		    	     Ext.MessageBox.alert("提示",result.msg);
		    	  }
			       
		    },
		    failure:function(response,options){
		        Ext.MessageBox.alert("提示","数据加载异常");
		    }
	 	});	    		 
	},
	funAfterShow:function(){
		var formPanel=this.partPropertyForm;
		var form=formPanel.getForm();
		if(this.params.data){
			form.setValues(this.params.data);
		}			
		if(this.params.type&&this.params.type=='view'){
			formPanel.form.items.each(function(field){
				field.setReadOnly(true);
				field.getEl().setStyle('background','#dddddd');
			});
			formPanel.ownerCt.buttons[0].hide();
		}else if(this.params.type&&this.params.type=='add'){		
			formPanel.enable();
			formBingingKeys(formPanel,this);
		}else if(this.params.type&&this.params.type=='edit'){
			formPanel.enable();
			form.findField("code").disable();
			form.findField("source").disable();
			formBingingKeys(formPanel,this);
		}
		
	},
	funClose:function(){
		this.close();
	}
});
Uec3.PartPropertyModule=Ext.extend(Ext.app.Module, {
	id : 'partPropertyOperation',
	/**
	 * Create this modules window here.
	 */
	createWindow : function(params) {
		var windowId="AddAndEditModule";
	   	if(params&&params.data&&params.data.id){
	   	   windowId+="_"+params.id;
	   	}
	    var desktop = Uec3.App.getDesktop();
	    var win = desktop.getWindowByUecId(windowId);
		if (!win) {
			win = desktop.createWindow({
				uecId:windowId,layout:'fit',width:800,closeAction:'hide',height:420,
			    params:params,iconCls : 'acc-icon',maximizable : true,resizable:false,
				maximized:false,title:"零件编辑-ESC关闭,ENTER向下选及保存",resizable :true,
				shadow : true,modal :true,closable:true,bodyStyle:'padding:5 5 5 5',animCollapse:true
			},Uec3.AddAndEditWindow);
		}else{
			win.params=params;
		}
		win.show();
	}
});